/*
 *	This file is part of Lounge-Cast.
 *
 *  Author:  	Markus Moormann
 *	E-mail:  	mmoorman@uni-osnabrueck.de
 *	Copyright:	University of Osnabrueck (Zentrum Virtuos)
 * 	Website:	www.uni-osnabrueck.de
 *	Date:		16.02.2011
 *
 *	Lounge-Cast is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *	Lounge-Cast is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Lounge-Cast.  If not, see <http://www.gnu.org/licenses/>.
 */
package multimedia.view.components.extended
{
	import flash.events.MouseEvent;
	
	import multimedia.model.LoginModel;
	import multimedia.model.vo.MediaVO;
	import multimedia.view.renderer.UserMediaRenderer;
	
	import mx.controls.List;
	import mx.controls.listClasses.IListItemRenderer;
	import mx.core.ClassFactory;

	public class UserMediaList extends List
	{
		public function UserMediaList()
		{
			super();
			itemRenderer = new ClassFactory(UserMediaRenderer);
			addEventListener(MouseEvent.CLICK, selectUserMedia);
		}
		
		private function selectUserMedia(event:MouseEvent):void
		{
			if (selectedItem && (selectedItem as MediaVO).approved)
			{
				selectedItem = null;
			}
		}

		/* override protected function drawHighlightIndicator(indicator:Sprite, 
														   x:Number, 
														   y:Number, 
														   width:Number, 
														   height:Number, 
														   color:uint, 
														   itemRenderer:IListItemRenderer):void
		{
			trace(color);
			if (!isItemSelected(itemRenderer.data))
			{
				if(itemRenderer.data.approved)
				{
					super.drawHighlightIndicator(indicator, x, y, unscaledWidth - viewMetrics.left - viewMetrics.right, height, 0xc4c4c4, itemRenderer);
				}else
				{
					super.drawHighlightIndicator(indicator, x, y, unscaledWidth - viewMetrics.left - viewMetrics.right, height, color, itemRenderer);
				}
			}
		} */
		override protected function mouseEventToItemRenderer(event:MouseEvent):IListItemRenderer
		{
			var listItem:IListItemRenderer = super.mouseEventToItemRenderer(event);
			if (listItem)
			{
				if (listItem.data)
				{
					if (listItem.data is MediaVO && 
						(listItem.data as MediaVO).approved)
					{
						return null;
					}
				}
			} 
			return listItem;
		}
		
		private function disabledFunction(item:Object):Boolean
		{
			if (item is MediaVO && (item as MediaVO).approved)
			{
				return true;
			}
			return false;
		}
		
		private var selectionUpward:Boolean;
		override protected function finishKeySelection():void
		{
			super.finishKeySelection();

			var i:int;
			var uid:String;
			var rowCount:int = listItems.length;
			var partialRow:int = (rowInfo[rowCount - 1].y + rowInfo[rowCount - 1].height > listContent.height) ? 1 : 0;

			var listItem:IListItemRenderer;
			listItem = listItems[caretIndex - verticalScrollPosition][0];
			if (listItem)
			{
				if (listItem.data)
				{
					if (disabledFunction(listItem.data))
					{
						// find another visible item that is enabled
						// assumes there is one that is fully visible
						rowCount = rowCount - partialRow;
						var idx:int = caretIndex - verticalScrollPosition;
						if (selectionUpward)
						{
							// look up;
							for (i = idx - 1; i >= 0; i--)
							{
								listItem = listItems[i][0];
								if (!disabledFunction(listItem.data))
								{
									selectedIndex = i - verticalScrollPosition;
									return;
								}
							}
							for (i = idx + 1; i < rowCount; i++)
							{
								listItem = listItems[i][0];
								if (!disabledFunction(listItem.data))
								{
									selectedIndex = i - verticalScrollPosition;
									return;
								}
							}
						}
						else
						{
							// look down;
							for (i = idx + 1; i < rowCount; i++)
							{
								listItem = listItems[i][0];
								if (listItem && !disabledFunction(listItem.data))
								{
									selectedIndex = i - verticalScrollPosition;
									return;
								}
							}
							for (i = idx - 1; i >= 0; i--)
							{
								listItem = listItems[i][0];
								if (listItem && !disabledFunction(listItem.data))
								{
									selectedIndex = i - verticalScrollPosition;
									return;
								}
							}
						}
					}
				}
			}
		}

	}
}